<?php

/**
* Calendar Helper for CakePHP
*
* Copyright 2007-2008 John Elliott
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
*
* @author John Elliott
* @copyright 2008 John Elliott
* @link http://www.flipflops.org More Information
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/

class CalendarHelper extends Helper
{

	var $helpers = array('Html', 'Form');
    public $type = '';

    private function _formatCell($data)
    {
        $out = '';
        switch($this->type) {
            case 'arrival':
                $out = $data['hour'] . ':' . $data['minute'];
                break;
            default :
        }

        return $out;
    }

    private function _formatClass($class, $data)
    {
        switch($this->type) {
            case 'arrival':
                if($data['hour'] < 8) {
                    $class .= 'arrival-ontime';
                } else {
                    $class .= 'arrival-late';
                }
                break;
            default :
        }

        return $class;
    }

/**
* Generates a Calendar for the specified by the month and year params and populates it with the content of the data array
*
* @param $year string
* @param $month string
* @param $data array
* @param $action
* @return string - HTML code to display calendar in view
*
*/

function table($year = '', $month = '', $data = '', $action ='')
	{
	$str = '';
	$month_list = array('january', 'febuary', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
	$day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
	$day = 1;
	$today = 0;

	if($year == '' || $month == '') { // just use current yeear & month
		$year = date('Y');
		$month = date('m');
	}



	//$flag = 0;
	//
	//for($i = 0; $i < 12; $i++) {
	//	if(strtolower($month) == $month_list[$i]) {
	//		if(intval($year) != 0) {
	//			$flag = 1;
	//			$month_num = $i + 1;
	//			break;
	//		}
	//	}
	//}
	//
	//if($flag == 0) {
	//	$year = date('Y');
	//	$month = date('F');
	//	$month_num = date('m');
	//}
    $month_num = $month;
	$next_year = $year;
	$prev_year = $year;

	$next_month = $month + 1;
	$prev_month = $month - 1;

	if($next_month == 13) {
		//$next_month = 'january';
        $next_month = 1;
		$next_year = intval($year) + 1;
	} else {
		//$next_month = $month_list[$next_month -1];
	}

	if($prev_month == 0) {
		$prev_month = 'december';
        $prev_month = 12;
        $prev_year = intval($year) - 1;
	} else {
		//$prev_month = $month_list[$prev_month - 1];
	}

	if($year == date('Y') && $month == date('m')) {
	// set the flag that shows todays date but only in the current month - not past or future...
		$today = date('j');
	}

	$days_in_month = date("t", mktime(0, 0, 0, $month_num, 1, $year));
	$first_day_in_month = date('D', mktime(0,0,0, $month_num, 1, $year));

	$str .= '<table class="calendar">';

	$str .= '<thead>';

	$str .= '<tr><th class="cell-prev">';

	$str .= $this->Html->link(__('prev', true), $action . '/' . $prev_year . '/' . $prev_month);

	$str .= '</th><th colspan="5">' . ucfirst($month_list[$month - 1]) . ' ' . $year . '</th><th class="cell-next">';

	$str .= $this->Html->link(__('next', true), $action . '/' . $next_year . '/' . $next_month);

	$str .= '</th></tr>';

	$str .= '<tr>';

		for($i = 0; $i < 7;$i++) {
				$str .= '<th class="cell-header">' . $day_list[$i] . '</th>';
		}

	$str .= '</tr>';

	$str .= '</thead>';

	$str .= '<tbody>';

	while($day <= $days_in_month) {
		$str .= '<tr>';

		for($i = 0; $i < 7; $i ++) {

			$cell = '&nbsp;';
			$class = '';

			if($i > 4) {
				$class .= ' cell-weekend ';
			}

			if($day == $today) {
				$class .= ' cell-today ';
			}

            if(($today != 0 && $day <= $today) || (date('Y') > $year || (date('Y') == $year && date('m') > $month))) {
                if(isset($data[$day])) {
                    $class = $this->_formatClass($class, $data[$day]);
                }
            }

			if(($first_day_in_month == $day_list[$i] || $day > 1) && ($day <= $days_in_month)) {
                if(($today != 0 && $day <= $today) || (date('Y') > $year || (date('Y') == $year && date('m') > $month))) {
                    if(isset($data[$day])) {
                        $cell = $this->_formatCell($data[$day]);
                    } else {
                        if($i <= 4) {
                            $class = 'absence';
                        }
                    }
                }
				$str .= '<td class="' . $class . '"><div class="cell-number">' . $day . '</div><div class="cell-data">' . $cell . '</div></td>';
				$day++;
			} else {
				$str .= '<td class="' . $class . '">&nbsp;</td>';
			}
		}
		$str .= '</tr>';
		}

	$str .= '</tbody>';

	$str .= '</table>';

	return $str;
	}
}

?>
